package saucaca.fragment;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.NewsAdapter;
import saucaca.CustomView.CustomListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.NewsObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class NewDetailFragment extends Fragment {
	MainActivity mActivity;
	List<NewsObject> listNew;
	private View mView_EmptyLoading = null;
	CustomListView listview;
	TextView textTitleConten, textShorttitle;
	WebView webView;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	String strKeyCode;
	LinearLayout newsOtherBar;
	NewsObject newsObject;
	NewsAdapter adapter;

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.news_detail, container, false);
		findViewById(view);
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
		adapter = new NewsAdapter(getActivity(), new ArrayList<NewsObject>(), NewDetailFragment.class.getName());
		Bundle b = getArguments();
		newsObject = (NewsObject) b.getSerializable(MainActivity.ID);
		strKeyCode = newsObject.getAUTO_ID();
		new GetFromServices().execute(strKeyCode);
		mActivity.myActionBar.setMenu(R.string.menu_tinamnhac, false, false);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(R.string.menu_tinamnhac, false, false);
	}

	public void findViewById(View view) {
		listview = (CustomListView) view.findViewById(R.id.new_detail_listview);
		listview.setExpanded(true);
		textTitleConten = (TextView) view.findViewById(R.id.new_detail_title);
		textShorttitle = (TextView) view.findViewById(R.id.new_detail_shortTitle);
		webView = (WebView) view.findViewById(R.id.new_detail_webView);
//		webView.setBackgroundColor(0);
		webView.setScrollbarFadingEnabled(true);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listview.setEmptyView(mView_EmptyLoading);
		newsOtherBar = (LinearLayout) view.findViewById(R.id.news_detail_barNewsOther);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	class GetFromServices extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.newsDetail(params[0]);
//			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			NewsObject newObj = null;
			if (result != null) {
				try {
					newObj = JsonReader.readNewsDetail(result);
					listNew = JsonReader.readNewsDetail_List(result);
					adapter = new NewsAdapter(getActivity(), listNew, NewDetailFragment.class.getName());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				newsOtherBar.setVisibility(View.VISIBLE);
				if (newObj != null)
					showContent(newObj);
			}
		}
	}

	void showContent(NewsObject newsObject) {
		if (newsObject != null) {
			textTitleConten.setText(newsObject.getTXNAME());
			// textShorttitle.setText(newsObject.getSHORT_DESC());
			final String mimeType = "text/html";
			final String encoding = "utf-8";
			String data =Html.fromHtml(newsObject.getCONTENTS()).toString();
			//TagName.showLogError("abc", newsObject.getCONTENTS());
			// data = newsObject.getCONTENTS().toString();
			data = data.replace("<img", "<img style=\"width:100%!important\"");
			webView.loadDataWithBaseURL(null,data, mimeType, encoding,null);
			listview.setAdapter(adapter);
		}
	}
}